#ifndef MYLIB_H
#define MYLIB_H

#include <stdio.h>
#include <assert.h>

#ifndef TRUE
#define TRUE			1
#define FALSE			0
#endif

#ifndef MIN
#define MIN(x,y) ((x) < (y))?(x):(y)
#define MAX(x,y) ((x) > (y))?(x):(y)
#endif

#ifndef NULL
#define NULL			((void *)0)
#endif

typedef int 	(*f_ptr)();

#ifndef BITSPERBYTE
#define BITSPERBYTE 8
#endif

#ifndef __BITS
#define __BITS(type)  (BITSPERBYTE * (int)sizeof(type))
#endif

#ifdef __TURBOC__
#define SIZE_T_IS_INT		1
#endif

#ifdef SIZE_T_IS_INT
typedef unsigned int INDEX;
#define AsciiToIndex atoi
#else 
typedef unsigned int INDEX;
#define AsciiToIndex atoi
#endif

#define MAX_INDEX ~(INDEX)0

#ifdef __STDC__	/* encompass ANSI and non-ANSI function prototypes */

#include <stdlib.h>

#define NOARGS	(void)
#define ARG1(t1, v1) (t1 v1)
#define ARG2(t1, v1, t2, v2) (t1 v1, t2 v2)
#define ARG3(t1, v1, t2, v2, t3, v3) (t1 v1, t2 v2, t3 v3)
#define ARG4(t1, v1, t2, v2, t3, v3, t4, v4) (t1 v1, t2 v2, t3 v3, t4 v4)
#define ARG5(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5) (t1 v1, t2 v2, t3 v3, t4 v4, t5 v5)
#define ARG6(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6) (t1 v1, t2 v2, t3 v3, t4 v4, t5 v5, t6 v6)
#define ARG7(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6, t7, v7) (t1 v1, t2 v2, t3 v3, t4 v4, t5 v5, t6 v6, t7 v7)

#define HARG1(t1, v1) (t1 v1)
#define HARG2(t1, v1, t2, v2) (t1 v1, t2 v2)
#define HARG3(t1, v1, t2, v2, t3, v3) (t1 v1, t2 v2, t3 v3)
#define HARG4(t1, v1, t2, v2, t3, v3, t4, v4) (t1 v1, t2 v2, t3 v3, t4 v4)
#define HARG5(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5) (t1 v1, t2 v2, t3 v3, t4 v4, t5 v5)
#define HARG6(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6) (t1 v1, t2 v2, t3 v3, t4 v4, t5 v5, t6 v6)
#define HARG7(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6, t7, v7) (t1 v1, t2 v2, t3 v3, t4 v4, t5 v5, t6 v6, t7 v7)

#else

#define const
#define NOARGS ()
#define ARG1(t1, v1) (v1) t1 v1;
#define ARG2(t1, v1, t2, v2) (v1, v2) t1 v1; t2 v2;
#define ARG3(t1, v1, t2, v2, t3, v3) (v1, v2, v3) t1 v1; t2 v2; t3 v3;
#define ARG4(t1, v1, t2, v2, t3, v3, t4, v4) (v1, v2, v3, v4) t1 v1; t2 v2; t3 v3; t4 v4;
#define ARG5(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5) (v1, v2, v3, v4, v5) t1 v1; t2 v2; t3 v3; t4 v4; t5 v5;
#define ARG6(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6) (v1, v2, v3, v4, v5, v6) t1 v1; t2 v2; t3 v3; t4 v4; t5 v5; t6 v6;
#define ARG7(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6, t7, v7) (v1, v2, v3, v4, v5, v6, v7) t1 v1; t2 v2; t3 v3; t4 v4; t5 v5; t6 v6; t7 v7;

#define HARG1(t1, v1) ()
#define HARG2(t1, v1, t2, v2) ()
#define HARG3(t1, v1, t2, v2, t3, v3) ()
#define HARG4(t1, v1, t2, v2, t3, v3, t4, v4) ()
#define HARG5(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5) ()
#define HARG6(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6) ()
#define HARG7(t1, v1, t2, v2, t3, v3, t4, v4, t5, v5, t6, v6, t7, v7) ()

extern int	abs	HARG1(int,	i); 
extern int	atoi	HARG1(char *,	c);
extern double	atof	HARG1(char *,	c);
extern long	atol	HARG1(char *,	c);
extern int	errno;
extern char	*getenv	HARG1(char *,	c);
extern char 	*strchr HARG2(char *, s, int, i);
#ifndef __malloc_h
extern char	*malloc	HARG1(int,	i);
extern int	free	HARG1(char *,	c);
extern char	*realloc HARG1(char *,	c);
#endif
#endif /* !__STDC__ */

/* Tools useful just about anywhere ... */
void	addcomma	HARG2(char *, str, long, num);
char	*field		HARG3(char *, line, int, num, char, separator);
void	fname		HARG2(char *, name, char *, filespec);
void	fqn		HARG2(char *, fullname, char *, fname);
void	re_fail		HARG2(char *, msg, char, op);
char	*re_comp	HARG1(char *, pat);
int	re_exec		HARG1(char *, lp);
void	re_modw		HARG1(char *, s);
int	re_subs		HARG2(char *, src, char *, dest);
void	undo_esc	HARG2(char *, dest, char *, src);
FILE	*pathopen	HARG3(char *, filename, char *, type, char *, fullname);

/* Tools useful in Turbo C */

#ifdef __TURBOC__
void	fixname		HARG1(char *, fname);
FILE	popen		HARG2(char *, command, char *, rw);
int	pclose		HARG1(FILE *, file);
void	zaptmp		();
#endif

/* Other common system things not declared elsewhere */
extern char *getwd  HARG1(char *, s);
extern char *mktemp HARG1(char *, s);

#ifdef __GNUC__
#ifndef alloca
#define alloca(ex) __builtin_alloca(ex)
#endif
#else
#ifndef _ALLOCA_H
extern char 		*alloca 	HARG1(size_t,	size);
#endif
#endif // __GNUC__

extern char 		*xmalloc	HARG1(INDEX,	size),
			*xrealloc	HARG2(char *, 	oldbuf,
					      INDEX,	size);

/* Safe STRING package */

struct STRING_S {
    int		len;
    int		maxlen;
    char	*str;
};
typedef struct STRING_S *STRING;

#define freeString(s)    	if (s) {if (s->str) free((s)->str); free(s);} s = NULL
#define StringChr(s, c)  	((s) && (s)->str)?strchr((s)->str, c): NULL
#define StringRChr(s, c) 	((s) && (s)->str)?strrchr((s)->str, c): NULL
#define StringStr(s, c)  	((s) && (s)->str)?strstr((s)->str, c): NULL
#define lastCharInString(s)	((s) && (s)->str && (s)->len)?(s)->str[(s)->len-1]:0

void	freeStringF	HARG1(STRING, s);
STRING 	newString 	HARG1(int,    len);
int 	StringLength 	HARG1(STRING, s);
int 	StringMaxLength HARG1(STRING, s);
char 	*StringToStr 	HARG1(STRING, s);
void	delCharInString	HARG2(STRING, dest, int, pos);
void	insCharInString	HARG3(STRING, dest, int, pos, char, p);
void	setCharInString	HARG3(STRING, dest, int, pos, char, p);
void 	truncString 	HARG2(STRING, dest, int, pos);
int 	trimString 	HARG1(STRING, dest);
int 	rTrimString 	HARG1(STRING, dest);
int 	lTrimString 	HARG1(STRING, dest);
STRING 	subString 	HARG3(STRING, source, int, start, int, end);
void	fillString	HARG2(STRING, dest, char, p);
void 	StringCpy 	HARG2(STRING, dest, STRING, source);
void 	StringCat 	HARG2(STRING, dest, STRING, source);
STRING 	StrToString 	HARG2(STRING, dest, char *, s);

struct ARRAY_S {
    void	**data;
    INDEX	len;
    INDEX	minlen; /* 'clump' - minimum increment for allocation */
    INDEX	maxlen;
};
typedef struct ARRAY_S *ARRAY;

#define sizeOfArray(a)	(a)->len
#define freeArray(a) if (a) { if ((a)->data) free((a)->data); free((a)); (a) = NULL; }

ARRAY newArray		HARG2(INDEX, len, INDEX, clump);
ARRAY addToArray	HARG2(ARRAY, a, void *, item);
ARRAY insertInArray	HARG3(ARRAY, a, void *, item, INDEX, itemnum);
ARRAY expandArray 	HARG2(ARRAY, a, INDEX, inc);
void setItem 		HARG3(ARRAY, a, INDEX, itemnum, void *, item);
void **removeFromArray	HARG2(ARRAY, a, INDEX, itemnum);
void **removeFromArrayp	HARG2(ARRAY, a, void **, itemp);
void removeAllItemsFromArray 
    			HARG2(ARRAY, a, f_ptr, destroyItem);
void *arrayItem		HARG2(ARRAY, a, INDEX, itemnum);
void **arrayItemp	HARG2(ARRAY, a, INDEX, itemnum);
void **prevArrayItemp 	HARG2(ARRAY, a, void **, itemp);
void **nextArrayItemp 	HARG2(ARRAY, a, void **, itemp);
void **firstArrayItemp 	HARG1(ARRAY, a);
void **lastArrayItemp 	HARG1(ARRAY, a);
INDEX arrayLength 	HARG1(ARRAY, a);
#endif /* MYLIB_H */

/* For emacs:
 * Local Variables:
 * tab-width: 8
 * End:
 */
